package dao

import (
	"context"
	"device-admin/internal/dal"
	"device-admin/internal/models"
	"time"

	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

type MaterialThemeConfig struct{}

func (MaterialThemeConfig) Create(ctx context.Context, db *gorm.DB, merchantID, themeID string, condition int, voice, emoji, light string) (err error) {
	m := dal.Use(db).MaterialThemeConfig
	var d models.MaterialThemeConfig
	d.ID, _ = helper.UUID{}.String()
	d.MerchantID = merchantID
	d.ThemeID = themeID
	d.Condition = condition
	d.Voice = voice
	d.Emoji = emoji
	d.Light = light
	d.CreatedAt = time.Now().Unix()
	m.WithContext(ctx).Create(&d)
	return nil
}

func (MaterialThemeConfig) List(ctx context.Context, db *gorm.DB, merchantID, themeID string, page, size int) (res []*models.MaterialThemeConfig, total int64, err error) {
	m := dal.Use(db).MaterialThemeConfig
	tx := m.WithContext(ctx)
	if merchantID != "" {
		tx = tx.Where(m.MerchantID.Eq(merchantID))
	}
	if themeID != "" {
		tx = tx.Where(m.ThemeID.Eq(themeID))
	}
	res, total, err = tx.Order(m.Condition).FindByPage(GetOffsetLimit(page, size))
	return
}

func (MaterialThemeConfig) Delete(ctx context.Context, db *gorm.DB, themeID string) (err error) {
	m := dal.Use(db).MaterialThemeConfig
	if themeID == "" {
		return
	}
	m.WithContext(ctx).
		Where(m.ThemeID.Eq(themeID)).
		Delete(&models.MaterialThemeConfig{})
	return nil
}
